package com.example.bbs.controller;

import com.example.bbs.domain.entity.Comment;
import com.example.bbs.domain.entity.Post;
import com.example.bbs.domain.model.CommentVO;
import com.example.bbs.domain.model.Page;
import com.example.bbs.domain.model.PostDTO;
import com.example.bbs.domain.model.ResponseResult;
import com.example.bbs.service.CommentService;
import com.example.bbs.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 帖子相关接口
 */
@Controller
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;
    @Autowired
    private CommentService commentService;
 


    /**
     * 打开发布帖子页面
     *
     * @return
     */
    @GetMapping("/toPublish")
    public String getPublishPage() {
        return "publish";
    }


    /**
     * 发布帖子
     *
     * @param post
     * @return
     */
    @PostMapping("/doPublish")
    @ResponseBody
    public ResponseResult publish(@Valid Post post) {
        postService.saveCache(post);
        return ResponseResult.success();
    }

    /**
     * 帖子详情
     *
     * @param postId
     * @param model
     * @param page
     * @return
     */
    @GetMapping("/detail/{postId}")
    public String detail(@PathVariable(name = "postId") String postId, Model model,
                         Page page, HttpServletRequest request) {
        //统计浏览量
        postService.incrPostViewCount(postId, request);
        //帖子详情
        PostDTO postDTO = postService.getPostDTO(postId);
        //帖子评论信息
        CommentVO commentVo = commentService.getBatchCommentForPost(postId, page.getPage(), page.getLimit());

        model.addAttribute("comments", commentVo);
        model.addAttribute("postDTO", postDTO);

        page.setPath("/detail/{postId}");
        page.setTotalSize(commentVo.getTotalSize());

        return "post";
    }

    /**
     * 帖子回复
     *
     * @param comment
     * @return
     */
    @PostMapping("/reply")
    @ResponseBody
    public ResponseResult reply(@Valid Comment comment) {
        commentService.saveCache(comment);
        //发送评论通知
        return ResponseResult.success("回复成功！");
    }


    /**
     * 帖子删除
     *
     * @param postId
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResponseResult delete(String postId) {
        postService.deleteCache(postId);
        return ResponseResult.success("删除成功");
    }

    /**
     * 帖子置顶
     *
     * @param postId
     * @return
     */
    @PostMapping("/setTop")
    @ResponseBody
    public ResponseResult setTop(String postId) {
        postService.setTop(postId);
        return ResponseResult.success("操作成功");
    }

    /**
     * 帖子加精
     *
     * @param postId
     * @return
     */
    @PostMapping("/setWonderful")
    @ResponseBody
    public ResponseResult setWonderful(String postId) {
        postService.setWonderful(postId);
        return ResponseResult.success("操作成功");
    }

}
